<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <!-- 搜索 clearable:清空数据，@clear:查询数据-->
        <el-input v-model="xx" placeholder="输入学号进行查询，查询结果为用户输入的学生"
          ><template #append>
            <el-button @click="getls()"
              ><el-icon><Search /></el-icon
            ></el-button>
          </template> </el-input
      ></el-col>
      <!--------------------------- 添加用户 -------------------------------->
      <el-col :span="4"><el-button type="primary" @click="addDialogVisible = true">添加用户</el-button> </el-col>
      <el-dialog title="添加用户" v-model="addDialogVisible" width="50%">
        <el-form label-width="70px">
          <el-form-item label="学号">
            <el-input v-model="sid" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="姓名">
            <el-input v-model="name" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="性别">
            <el-input v-model="gender" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="学院">
            <el-input v-model="college" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="系别">
            <el-input v-model="sdept" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="班级">
            <el-input v-model="class1" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="insertstudent()">确 定</el-button>
        </span>
      </el-dialog>
    </el-row>

    <el-table :data="user">
      <el-table-column type="index"></el-table-column>

      <el-tableColumn label="学号" prop="sid"></el-tableColumn>
      <el-tableColumn label="姓名" prop="name"></el-tableColumn>
      <el-tableColumn label="性别" prop="gender"></el-tableColumn>
      <el-tableColumn label="学院" prop="college"></el-tableColumn>
      <el-tableColumn label="系别" prop="sdept"></el-tableColumn>
      <el-tableColumn label="班级" prop="class"></el-tableColumn>

      <el-tableColumn label="操作" width="180px">
        <template v-slot="scope">
          <el-button round type="primary" size="mini" @click="editDialogVisible = true"
            ><el-icon><Edit /></el-icon
          ></el-button>

          <el-button round type="danger" size="mini" @click="del(scope.row)"
            ><el-icon><Delete /></el-icon
          ></el-button>
        </template>
      </el-tableColumn>
    </el-table>
    <el-dialog title="修改用户资料" v-model="editDialogVisible" width="50%">
      <!-- 添加用户内容底部区域 -->
      <el-form label-width="70px">
        <el-form-item label="学号">
          <el-input v-model="updatesid" placeholder="您要修改的该学生学号"></el-input>
        </el-form-item>

        <el-form-item label="姓名">
          <el-input v-model="updatename" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-input v-model="updategender" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="学院">
          <el-input v-model="updatecollege" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="系别">
          <el-input v-model="updatesdept" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="班级">
          <el-input v-model="updateclass1" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="editDialogVisible = false">取 消</el-button>
        <el-button round type="primary" @click="updatestudent()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="删除用户资料" v-model="deleteDialogVisible" width="50%">
      <!-- 添加用户内容底部区域 -->
      <el-form label-width="70px">
        <el-form-item label="学号">
          <el-input v-model="deletesid" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="deleteDialogVisible = false">取 消</el-button>
        <el-button round type="primary" @click="deletestudent(e)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { h } from 'vue'
import { ElNotification } from 'element-plus'
import { ref } from 'vue'
export default {
  data() {
    return {
      sid: ref(''),
      gender: ref(''),
      name: ref(''),
      class1: ref(''),
      college: ref(''),
      sdept: ref(''),

      updatesid: ref(''),
      updategender: ref(''),
      updatename: ref(''),
      updateclass1: ref(''),
      updatecollege: ref(''),
      updatesdept: ref(''),

      deletesid: ref(''),

      addDialogVisible: false,
      editDialogVisible: false,
      deleteDialogVisible: false,
      user: [],
      xx: ref(''),
    }
  },
  created() {
    this.getls()
  },
  methods: {
    // 获取用户列表数据（getUserList）
    getls() {
      const that = this
      this.$axios.get('/getstudent', { params: { xx: this.xx } }).then(function (response) {
        console.log(response)
        that.user.push(response['data'])
      })
    },
    del(row) {
      console.log(row)
      this.deleteDialogVisible = true
    },
    addDialogClosed() {
      this.$refs.addUserFormRef.resetFields()
    },
    editDialogClosed() {
      this.$refs.editUserFormRef.resetFields()
    },
    deleteDialogClosed() {
      this.$refs.editUserFormRef.resetFields()
    },
    insertstudent() {
      const that = this
      this.$axios
        .get('/insertstudent', {
          params: {
            sid: this.sid,
            name: this.name,
            gender: this.gender,
            sdept: this.sdept,
            class1: this.class1,
            college: this.college,
          },
        })
        .then(function (response) {
          that.addDialogVisible = false
          that.$message({
            message: h('i', { style: 'color: coral' }, '用户已插入数据库'),
          })
        })
    },
    updatestudent() {
      const that = this
      this.$axios
        .get('/updatestudent', {
          params: {
            sid: this.updatesid,
            name: this.updatename,
            gender: this.updategender,
            sdept: this.updatesdept,
            class1: this.updateclass1,
            college: this.updatecollege,
          },
        })
        .then(function (response) {
          that.$message({
            message: '用户已更新',
          })
        })
    },
    deletestudent(e) {
      console.log(e)
      const that = this
      this.$axios
        .get('/deletestudent', {
          params: {
            sid: this.deletesid,
          },
        })
        .then(function (response) {
          that.$message({
            type: 'success',
            message: '用户已在数据库删除',
          })
        })
    },
  },
}
</script>

<style lang="less" scoped></style>
